package com.xiaotu.spider.guduo.pageprocessor;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.GuDuoDataModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.JsonPathSelector;
@Component("GuDuoNewDataPageProcessor")
public class GuDuoNewDataPageProcessor extends AUrlPageProcessor{
	
	private static String date = GuDuoNewDataPageProcessor.getNextDay();
	
	private static String today = GuDuoNewDataPageProcessor.getToDay();
	
	private static String[] URL = new String[]{
			//连续剧
			"http://data.guduomedia.com/show/datalist?category=DRAMA&date="+date+"&t=1517800059411",
			//综艺
			"http://data.guduomedia.com/show/datalist?category=VARIETY&date="+date+"&t=1517800060171",	
			//网络剧
			"http://data.guduomedia.com/show/datalist?category=NETWORK_DRAMA&date="+date+"&t=1517800062427",
			//网络大电影
			"http://data.guduomedia.com/show/datalist?category=NETWORK_MOVIE&date="+date+"&t=1517800063654",
			//网络综艺
			"http://data.guduomedia.com/show/datalist?category=NETWORK_VARIETY&date="+date+"&t=1517800064350",
			//电视剧
			"http://data.guduomedia.com/show/datalist?category=TV_DRAMA&date="+date+"&t=1517800067275",
			//电视综艺
			"http://data.guduomedia.com/show/datalist?category=TV_VARIETY&date="+date+"&t=1517800067907",
			//国漫
			"http://data.guduomedia.com/show/datalist?category=ANIME&date="+date+"&t=1517800068715",
	};
	long count = 0;
	@Override
	public void process(Page page) {
		String requestUrl = page.getRequest().getUrl().toString();
		
		if(requestUrl.contains("http://data.guduomedia.com/show/datalist?category=")){
		//获得是哪种类型
		String tvtype = requestUrl.substring(requestUrl.indexOf("category=")+9, requestUrl.indexOf("&date="));
		
		switch (tvtype){
			case "DRAMA":addNewRequest("连续剧","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			case "VARIETY":addNewRequest("综艺","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			case "NETWORK_DRAMA":addNewRequest("网络剧","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			case "NETWORK_MOVIE":addNewRequest("网络大电影","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			case "NETWORK_VARIETY":addNewRequest("网络综艺","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			case "TV_DRAMA":addNewRequest("电视剧","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			case "TV_VARIETY":addNewRequest("电视综艺","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			case "ANIME":addNewRequest("国漫","http://data.guduomedia.com/show/detail?showId=",page);
				break;
			}
		}
		//是否是详情页
		if(requestUrl.contains("http://data.guduomedia.com/show/detail?showId=")){
			getTvInfo(requestUrl,page);
		}
		
	}

	//用于添加新的请求
	public void addNewRequest(String tvtype,String url,Page page){
		Map<String,Object> map = new HashMap<>();
		map.put("tvtype", tvtype);
		//获取showId
		List<String> showIdList = new JsonPathSelector("$.data.dayBillboardList[*].showId").selectList(page.getRawText());
		for (String showId : showIdList) {
			Request request = new Request(url+showId);
			request.setExtras(map);
			page.addTargetRequest(request);
		}
	}
	//获取具体信息
	public void getTvInfo(String requestUrl,Page page){
		//获取showId
		String showId = requestUrl.substring(requestUrl.lastIndexOf("=")+1);
		//获取当前类型tvType
		String tvType = page.getHtml().xpath("//div[@class='clearfix title-box']/p[@class='tag fl']/text()").get();
		//获取剧名
		String tvname = page.getHtml().xpath("//div[@class='clearfix title-box']/h2[@class='fl']/text()").get();
		//获取今日点击量
		String today_play_count = null;
		//获取昨日点击量
		String yday_play_count = null;
		//获取总点击量
		String total_play_count = null;
		//获取上映天数
		int release_days = 0;
		//获取网站集
		String [] play_rate_name = null;
		//获得网站对应的点击量比例集
		String [] play_rate_count = null;
		//获取网站名
		String resourceid = null;
		//网站今日点播量
		String today_play_count_branch = null;
		//用于统计比例总数
		String sumCount = null;
		String httpGetJSONPClick = HttpUtils.httpGetJSONP("http://data.guduomedia.com/playcount/review/"+showId);
		if (!StringUtils.isEmpty(httpGetJSONPClick)){
			JSONObject playCountObject = JSONObject.fromObject(httpGetJSONPClick);
			today_play_count = playCountObject.get("today_play_count").toString();
			yday_play_count = playCountObject.get("yesterday_play_count").toString();
			total_play_count = playCountObject.get("total_play_count").toString();
			release_days = (int)playCountObject.get("release_days");
		}
		String httpGetJSONPResourceid = HttpUtils.httpGetJSONP("http://data.guduomedia.com/playcount/day/"+showId+"?day=1");
		
		if(!StringUtils.isEmpty(httpGetJSONPResourceid)){
			@SuppressWarnings("static-access")
			JSONObject my = new JSONObject().fromObject(httpGetJSONPResourceid);
			String rateName = my.getString("names");//["爱奇艺"]
			String rateCount = my.getString("clicks");//[[1798240]]
			if(StringUtils.isNotEmpty(rateName) && StringUtils.isNotEmpty(rateCount)){
				if(rateName.contains("PP视频")){
					rateName=rateName.replace("PP视频", "PPTV聚力");
	    		}
				String [] rateNameArr = rateName.replace("[", "").replace("]", "").replace("\"", "").split(",");
				String [] rateCountArr = rateCount.replace("[", "").replace("]", "").split(",");
				play_rate_name = new String[rateNameArr.length];
				play_rate_count = new String[rateCountArr.length];
				for (int i = 0; i < rateNameArr.length; i++) {
					play_rate_name[i]= rateNameArr[i];
					play_rate_count[i]= rateCountArr[i];
				}
				
			}
		}
		List<GuDuoDataModel> guDuoDataList = new ArrayList<>();
		//求出各个网站今日点击量保存至pipeline
		if(play_rate_name.length>0 && play_rate_count.length>0 && today_play_count != null){
			for (int i = 0; i < play_rate_name.length; i++) {
				count++;	
				today_play_count_branch = play_rate_count[i];
				//System.out.println("影视剧名："+tvname+"影视剧类型："+tvType+"今日总点击量："+today_play_count+"昨日点击量："+yday_play_count+"总点击量："+total_play_count+"上映天数："+release_days+"网站名称："+play_rate_name[i]+"今日点击量："+play_rate_count[i]);
				guDuoDataList.add(new GuDuoDataModel(today+count,tvname,today_play_count,yday_play_count,total_play_count,release_days,play_rate_name,play_rate_count,play_rate_name[i],today_play_count_branch,DateUtils.getLastDayLongType(System.currentTimeMillis())));
			}
	     }else{
				play_rate_name = new String[]{};
				play_rate_count = new String[]{};
				today_play_count_branch = "";
				count++;
				guDuoDataList.add(new GuDuoDataModel(today+count,tvname,today_play_count,yday_play_count,total_play_count,release_days,play_rate_name,play_rate_count,"",today_play_count_branch,DateUtils.getLastDayLongType(System.currentTimeMillis())));
			}	
		
		//导出csv文件
    	page.putField(GuDuoDataModel.class.getName() + "List", guDuoDataList);
	}
	
	
	@Override
	protected String[] getStartRequestUrls() {
		return URL;
	}

	@Override
	protected String getSpiderType() {
		return "GUDUO_NetTV";
	}

	@Override
	public String getPageDataType(Page page) {
		return Constants.SpiderTaskType.TV_DATA_GUDUO;
	}
	//获取前一天
	public static String getNextDay() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -1);  
        date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);  
        return format;  
    } 
	public static String getToDay(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String format = sdf.format(date);
		return format;
	}
}
